//
//  OrderManageViewController.m
//  Mahjong
//
//  Created by mc on 16/9/9.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "OrderManageViewController.h"
#import "WaitDealViewController.h" //待受理详情
#import "YiBaojiaViewController.h" //已报价详情
#import "OrderManagerTableViewCell.h"
#import "Header.h"
#import "OrderModel.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface OrderManageViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *buttonArr;
    NSInteger btnTag;
}

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL updata;
@end

@implementation OrderManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithWhite:0.900 alpha:1.000];
    buttonArr = [[NSArray alloc] initWithObjects:@"待受理",@"已报价",@"待完成",@"已完成", nil];
    
//    [self addNav];
    [self addOrderStatesUI];
    [self addTableviewUI];
    self.navigationItem.title = @"订单管理";
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    self.dataArray = [NSMutableArray array];
    self.page = 1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(head)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(foot)];
    
}
-(void)head{
    self.updata = YES;
    [self create:self.buttonNumber pag:1];

}
-(void)foot{
    self.page++;
    self.updata = NO;
    [self create:self.buttonNumber pag:self.page];

    
}
-(void)create:(NSInteger)num pag:(NSInteger)page{
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetRepair&page=%ld&userid=%@&state=%ld",page,USERID,num-2000] success:^(id result) {
        NSLog(@"%@",result);
        if (self.updata) {
            [self.dataArray removeAllObjects];

        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            OrderModel *model = [[OrderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

// 处理导航栏
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}
-(void)addNav{
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 40)];
    _backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_backView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"nav_but_back"] forState:UIControlStateNormal];
    backBtn.backgroundColor = [UIColor clearColor];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(20, 5, 25, 25);
    [_backView addSubview:backBtn];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 2 / 5 - 8, 0, WIDTH / 5, 40)];
    lable.backgroundColor = [UIColor whiteColor];
    lable.text = @"订单管理";
    lable.font = [UIFont systemFontOfSize:20];
    lable.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:lable];
    
}
-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}
// 第二部分订单状态
-(void)addOrderStatesUI{
    UIView *statesView = [[UIView alloc] initWithFrame:CGRectMake(0,  2, WIDTH, 40)];
    statesView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:statesView];
    
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(WIDTH/4*i, 0, WIDTH/4, 40);
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:buttonArr[i] forState:UIControlStateNormal];
        btn.tag = 2000 + i;
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
#pragma mark -- 修改
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        if (btn.tag == self.buttonNumber) {
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            btnTag = self.buttonNumber;
        }
        
        
        [statesView addSubview:btn];
    }
}
-(void)btnAction:(UIButton *)button {
    self.buttonNumber = button.tag;
//    NSLog(@"%ld",button.tag);
    self.updata = YES;
    [self create:button.tag pag:1];
    btnTag = button.tag;
    [self setBtnColor];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    button.selected = !button.selected;
}
- (void)setBtnColor
{
    for (int i = 2000; i < 2004; i++) {
        UIButton *btn = (UIButton*)[self.view viewWithTag:i];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
}

// 维修信息
- (void)addTableviewUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.backView.frame) + 42 + 5, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.900 alpha:1.000];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, HEIGHT/4, 0);
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.tableView];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"RepairTableviewCell";
    OrderManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[OrderManagerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    OrderModel *model = self.dataArray[indexPath.row];
    cell.labelName.text = @"芎翊 2016-06-07-13:00";
    cell.labelName.text = [NSString stringWithFormat:@"%@ %@",model.name,model.addtime];
    cell.labelContent.text = @"麻将机卡坏了, 不能洗牌";
    cell.labelContent.text = model.conetnts;
    cell.labelNumber.text = @"订单号:6595428445";
    cell.labelNumber.text = [NSString stringWithFormat:@"订单号:%ld",model.OrderId];
    cell.labelData.text = @"2016-06-07-14:00";
    cell.labelData.text = model.addtime;
    [cell.imgViewHead sd_setImageWithURL:[NSURL URLWithString:[model.photourl stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
//    cell.backgroundColor = [UIColor redColor];
    if (model.imagelist.count == 1) {
        [cell.imgViewPOne sd_setImageWithURL:[NSURL URLWithString:[[model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        cell.imgViewPTwo.alpha = 0;
        cell.imgViewPThree.alpha = 0;
    }
    if (model.imagelist.count == 2) {
        [cell.imgViewPOne sd_setImageWithURL:[NSURL URLWithString:[[model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        [cell.imgViewPTwo sd_setImageWithURL:[NSURL URLWithString:[[model.imagelist[1] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        cell.imgViewPThree.alpha = 0;
    }
    if (model.imagelist.count == 3) {
//        NSString *s = [model.imagelist[0] objectForKey:@"imageurl"];
//        NSString *newImgStr =  [[model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com"];
        [cell.imgViewPOne sd_setImageWithURL:[NSURL URLWithString:[[model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        [cell.imgViewPTwo sd_setImageWithURL:[NSURL URLWithString:[[model.imagelist[1] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        [cell.imgViewPThree sd_setImageWithURL:[NSURL URLWithString:[[model.imagelist[2] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        
    }
   
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT/3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerview = [[UIView alloc] init];
    headerview.backgroundColor = [UIColor colorWithWhite:0.900 alpha:1.000];
    
    return headerview;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
//    ReadyBaojiaViewController *readyBaojiaVC = [[ReadyBaojiaViewController alloc] init];
//    [self.navigationController pushViewController:readyBaojiaVC animated:YES];
    OrderModel *model = self.dataArray[indexPath.row];

    if (0 == btnTag || 2000 == btnTag) {// 待受理订单详情
        WaitDealViewController *waitDealVC = [[WaitDealViewController alloc] init];
        
        waitDealVC.num  = model.OrderId;
        [self.navigationController pushViewController:waitDealVC animated:YES];
        
    }else if (2001 == btnTag){ //已报价订单详情
        YiBaojiaViewController *yibaojiaVC = [[YiBaojiaViewController alloc] init];
        yibaojiaVC.mark = 1;
        yibaojiaVC.num = model.OrderId;
        [self.navigationController pushViewController:yibaojiaVC animated:YES];
    }else if (2002 == btnTag){ //待完成订单详情
        YiBaojiaViewController *yibaojiaVC = [[YiBaojiaViewController alloc] init];
        yibaojiaVC.mark = 2;
        yibaojiaVC.num = model.OrderId;

        [self.navigationController pushViewController:yibaojiaVC animated:YES];
    }else if (2003 == btnTag){ //已完成订单详情
        YiBaojiaViewController *yibaojiaVC = [[YiBaojiaViewController alloc] init];
        yibaojiaVC.mark = 3;
        yibaojiaVC.num = model.OrderId;

        [self.navigationController pushViewController:yibaojiaVC animated:YES];
    }
}





////第三部分 订单内容
//-(void)addOrderContent{
//    
//    UIButton *statesView = [[UIButton alloc] initWithFrame:CGRectMake(0, 102 + 10, WIDTH, HEIGHT/3)];
//    statesView.backgroundColor = [UIColor whiteColor];
//    [statesView addTarget:self action:@selector(ordereContentAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:statesView];
//    
//    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, HEIGHT/12, HEIGHT/12)];
//    imgView.backgroundColor = [UIColor grayColor];
//    imgView.image = [UIImage imageNamed:@""];
//    imgView.layer.cornerRadius = imgView.frame.size.height/2;
//    [statesView addSubview:imgView];
//    
//    UILabel *nameLable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/12 + 20,  10, WIDTH / 3, HEIGHT/24)];
////    nameLable.backgroundColor = [UIColor redColor];
//    nameLable.text = @"穹翊";
////    nameLable.textAlignment = NSTextAlignmentCenter;
//    [statesView addSubview:nameLable];
//    
//    UILabel *contentLable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/12 + 20, HEIGHT/24 + 10, WIDTH/2, HEIGHT/24)];
////    contentLable.backgroundColor = [UIColor redColor];
//    contentLable.text = @"麻将机卡坏了, 不能洗牌了";
////    contentLable.textAlignment = NSTextAlignmentCenter;
//    [statesView addSubview:contentLable];
//    
//    for (NSInteger i = 0; i < 3; i++) {
//        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((10+WIDTH/4)*i + 15, HEIGHT/12 + 20, WIDTH / 4, WIDTH/4)];
//        imgView.backgroundColor = [UIColor grayColor];
//        imgView.image = [UIImage imageNamed:@""];
//        [statesView addSubview:imgView];
//    }
//    
//    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,  HEIGHT/12 + 20 + WIDTH/4 + 10, WIDTH / 2, HEIGHT/24)];
////    numberLabel.backgroundColor = [UIColor redColor];
//    numberLabel.text = @"订单号: 6595425845";
//    numberLabel.font = [UIFont systemFontOfSize:15];
//    numberLabel.textColor = [UIColor grayColor];
//    //    nameLable.textAlignment = NSTextAlignmentCenter;
//    [statesView addSubview:numberLabel];
//    
//    UILabel *dataLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + WIDTH/2,  HEIGHT/12 + 20 + WIDTH/4 + 10, WIDTH / 3, HEIGHT/24)];
////    dataLabel.backgroundColor = [UIColor redColor];
//    dataLabel.text = @"2016-06-07-14:00";
//    dataLabel.font = [UIFont systemFontOfSize:13];
//    dataLabel.textColor = [UIColor grayColor];
//    //    nameLable.textAlignment = NSTextAlignmentCenter;
//    [statesView addSubview:dataLabel];
//
//}
//-(void)ordereContentAction{
//    NSLog(@"%ld",btnTag);
//    if (0 == btnTag || 2000 == btnTag) {// 待受理订单详情
//        WaitDealViewController *waitDealVC = [[WaitDealViewController alloc] init];
//        [self.navigationController pushViewController:waitDealVC animated:YES];
//        
//    }else if (2001 == btnTag){ //已报价订单详情
//        YiBaojiaViewController *yibaojiaVC = [[YiBaojiaViewController alloc] init];
//        yibaojiaVC.mark = 1;
//        [self.navigationController pushViewController:yibaojiaVC animated:YES];
//    }else if (2002 == btnTag){ //待完成订单详情
//        YiBaojiaViewController *yibaojiaVC = [[YiBaojiaViewController alloc] init];
//        yibaojiaVC.mark = 2;
//        [self.navigationController pushViewController:yibaojiaVC animated:YES];
//    }else if (2003 == btnTag){ //已完成订单详情
//        YiBaojiaViewController *yibaojiaVC = [[YiBaojiaViewController alloc] init];
//        yibaojiaVC.mark = 3;
//        [self.navigationController pushViewController:yibaojiaVC animated:YES];
//    }
//}

//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:YES];
//    self.navigationController.navigationBarHidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
//}
//-(void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:YES];
//    self.navigationController.navigationBarHidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
